<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use backend\models\User;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use backend\models\LoginForm;
use backend\models\RegisterForm;

/**
 * Site controller
 */
class SiteController extends Controller
{
    const TX_AX = [
        1 => '你好', 2 => '我不好'
    ];

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error', 'register', 'check-mobile-exists', 'check-sms-code-exists'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Register action
     *
     * @return string|\yii\web\Response
     */
    public function actionRegister()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }
        $model = new RegisterForm();
        if ($model->load(Yii::$app->request->post()) && Yii::$app->session->get('register_sms_code') == Yii::$app->request->post('RegisterForm')['register_sms_code'] && $user = $model->register()) {
            if (Yii::$app->getUser()->login($user)) {
                return $this->goHome();
            }
        }
        return $this->render('register', [
            'model' => $model,
        ]);
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    /**
     * CheckMobileExists action
     * 验证手机号是否存在
     * @return string
     */
    public function actionCheckMobileExists()
    {
        $mobile = Yii::$app->request->post('mobile');
        if (!User::findByMobile($mobile)) {
            echo 'Success';
        } else {
            echo 'Failed';
        }
    }

    /**
     * CheckSmsCodeExists action
     * 验证手机验证码是否正确
     * @return string
     */
    public function actionCheckSmsCodeExists()
    {
        $register_sms_code = Yii::$app->request->post('register_sms_code');
        if ($register_sms_code == Yii::$app->session->get('register_sms_code')) {
            echo 'Success';
        } else {
            echo 'Failed';
        }
    }
}
